package xyz.xpman.prisonerfitness.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;
import java.util.Objects;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.adapter.TrainingRecordAdapter;
import xyz.xpman.prisonerfitness.entity.TrainingRecord;
import xyz.xpman.prisonerfitness.service.TrainingRecordService;

/**
 * 锻炼记录
 */
public class TrainingRecordsActivity extends BaseActivity {
    TextView trainingTime;
    TextView trainingDays;
    TextView trainingTimes;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_records);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        initView();
    }

    private void initView() {
        // 累计训练时间
        trainingTime = findViewById(R.id.training_times);
        // 累计训练天数
        trainingDays = findViewById(R.id.training_days);
        // 累计训练完成次数
        trainingTimes = findViewById(R.id.finish_times);
    }


    @Override
    protected void onResume() {
        super.onResume();


        int trainingTime = TrainingRecordService.ME.countTrainingTime();
        this.trainingTime.setText(String.valueOf(trainingTime));


        int trainingDays = TrainingRecordService.ME.countTrainingDays();
        this.trainingDays.setText(String.valueOf(trainingDays));


        int trainingTimes = TrainingRecordService.ME.countTrainingTimes();
        this.trainingTimes.setText(String.valueOf(trainingTimes));


        processRecords();
    }


    @SuppressLint("NewApi")
    private void processRecords() {
        List<TrainingRecord> records = TrainingRecordService.ME.findAll();
        Log.i("TrainingRecordsActivity", "TrainingRecords: " + records);


        ListView listView = findViewById(R.id.training_record);


        ListAdapter adapter = new TrainingRecordAdapter(
                this, R.layout.training_record_list_item_temp, records);


        runOnUiThread(() -> listView.setAdapter(adapter));
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }


}